package com.sss.gsb.orm.repository;

import java.text.MessageFormat;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.sss.gsb.orm.model.DeadMan;
import com.sss.gsb.orm.model.RecordStatus;
@Repository
public class DeadManRepository extends BaseRepository<DeadMan>
	implements IGSBRepository<DeadMan>{

	
	@Override
	protected Class<DeadMan> getPersistantClass() {
		return DeadMan.class;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DeadMan> findByCriteria(String idCard, String name, String surname,
			String cpk,String cps) throws Exception {

		String stm = MessageFormat.format(STM, getPersistantClass().getSimpleName());
		Map<String, Object> param = new Hashtable<String, Object>();
		param.put("rec", RecordStatus.N);

		if (!StringUtils.isBlank(idCard)) {
			stm = stm.concat("and m.customer.idCard = :idcard ");
			param.put("idcard", idCard);
		}
		if (!StringUtils.isBlank(name)) {
			stm = stm.concat("and m.customer.name = :name ");
			param.put("name", name);
		}
		if (!StringUtils.isBlank(surname)) {
			stm = stm.concat("and m.customer.surname = :surname");
			param.put("surname", surname);
		}
		
		/* cpk and cps are mached into fields (HTEACH, HMEM)*/
		if (!StringUtils.isBlank(cpk)) {
			stm = stm.concat("and m.customer.hTaech = :cpk ");
			param.put("cpk", cpk);
		}
		
		if (!StringUtils.isBlank(cps)) {
			stm = stm.concat("and m.customer.hMem = :cps ");
			param.put("cps", cps);
		}
		
		Query query = getCurrentSession().createQuery(stm);
		
		this.mapQueryParameter(param, query);


		return (List<DeadMan>)query.list();
	}

}
